//
//  HWGameCommentSonTopCell.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/3/24.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit
import Cosmos
class HWGameCommentSonTopCell: UITableViewCell {
    var headImageView:UIImageView!
    var headFrameImageView:UIImageView!
    var nameLabel:GranibleLabel!
    var medalImageView:UIImageView!
    var vipImageView:UIImageView!
    var starView:CosmosView!
    var contentLabel:UILabel!
    var videoImageView:UIImageView!
    var videoPlayImageView:UIImageView!
    var videoButton:UIButton!
    var timeLabel:UILabel!
    var commentImageView:UIImageView!
    var zanImageView:UIImageView!
    var commentLabel:UILabel!
    var zanLabel:UILabel!
    var commentButton:UIButton!
    var zanButton:UIButton!
    var lineImageView:UIImageView!
    var imageViews:[UIImageView] = []
    var imageViewButtons:[UIButton] = []
    let photoBroseView = PYPhotoBrowseView()
    var zanButtonClick:((_:String)->())?
    var videoPlayClick:((_:String)->())?
    var model:HWGameDetailCommentListModel? {
        didSet{
            
            headImageView.sd_setImage(with: URL(string: model?.touxiang ?? ""), completed: nil)
            headFrameImageView.sd_setImage(with: URL(string: model?.head_frame ?? ""), completed: nil)
            medalImageView.sd_setImage(with: URL(string: model?.medal ?? ""), completed: nil)
            vipImageView.sd_setImage(with: URL(string: model?.huiyuan_img ?? ""), completed: nil)
            contentLabel.text = model?.content
            nameLabel.text = model?.user_name
            timeLabel.text = model?.time
            commentLabel.text = model?.num
            zanLabel.text = model?.zan
            if model?.is_zan == true {
                zanImageView.image = UIImage(named: "zan_select")
            }else {
                zanImageView.image = UIImage(named: "zan_normal")
            }
            starView.rating = model?.point ?? 0.0
            if (model?.color.count ?? 0) > 1 {
                var arr:[UIColor] = Array()
                for color in model?.color ?? [] {
                    arr.append(UIColor.colorWithHexString(hexString: color))
                }
                nameLabel.colors = arr
            }else {
                nameLabel.colors = [UIColor.colorWithHexString(hexString: model?.color[0] ?? "#333333"),UIColor.colorWithHexString(hexString: model?.color[0] ?? "#333333")]
            }
            
            if (model?.video.count ?? 0) > 0 {
                videoPlayImageView.isHidden = false
                videoImageView.isHidden = false
                if (model?.images.count ?? 0) > 0 {
                    videoImageView.sd_setImage(with: URL(string: model?.images[0] ?? ""), completed: nil)
                }
                
                videoImageView.snp.remakeConstraints { make in
                    make.left.equalTo(nameLabel)
                    make.top.equalTo(contentLabel.snp.bottom).offset(10*widthScale)
                    make.right.equalTo(contentLabel)
                    make.height.equalTo(videoImageView.snp.width).multipliedBy(0.56)
                }
                
                
                    timeLabel.snp.remakeConstraints { make in
                        make.left.equalTo(nameLabel)
                        make.top.equalTo(videoImageView.snp.bottom).offset(13*widthScale)
                    }
             
            }else {
                videoPlayImageView.isHidden = true
                videoImageView.isHidden = true
                imageViews.removeAll()
                imageViewButtons.removeAll()
                if (model?.images.count ?? 0) > 0 {
                    var tempImgView:UIImageView!
                    for i in 0..<model!.images.count{
                        let imgView = UIImageView()
                        imgView.layer.cornerRadius = 5*widthScale
                        imgView.layer.masksToBounds = true
                        imgView.contentMode = .scaleAspectFill
                        imgView.sd_setImage(with: URL(string: model!.images[i]), completed: nil)
                        contentView.addSubview(imgView)
                        
                        let button = UIButton(type: .custom)
                        button.tag = 9999 + i
                        button.addTarget(self, action: #selector(buttonClick(_:)), for: .touchUpInside)
                        contentView.addSubview(button)
                        
                        if tempImgView != nil {
                            if i % 3 == 0 {
                                imgView.snp.makeConstraints { make in
                                    make.left.equalTo(nameLabel)
                                    make.top.equalTo(tempImgView.snp.bottom).offset(10*widthScale)
                                    make.width.equalTo(92*widthScale)
                                    make.height.equalTo(imgView.snp.width).multipliedBy(0.8)
                                }
                            }else {
                                imgView.snp.makeConstraints { make in
                                    make.left.equalTo(tempImgView.snp.right).offset(10*widthScale)
                                    make.top.equalTo(tempImgView)
                                    make.width.equalTo(92*widthScale)
                                    make.height.equalTo(imgView.snp.width).multipliedBy(0.8)
                                }
                            }
                        }else {
                            imgView.snp.makeConstraints { make in
                                make.left.equalTo(nameLabel)
                                make.top.equalTo(contentLabel.snp.bottom).offset(10*widthScale)
                                make.width.equalTo(92*widthScale)
                                make.height.equalTo(imgView.snp.width).multipliedBy(0.8)
                            }
                        }
                        button.snp.makeConstraints { make in
                            make.left.right.top.bottom.equalTo(imgView)
                        }
                        tempImgView = imgView
                        imageViews.append(imgView)
                        
                        imageViewButtons.append(button)
                    }
                }
                
                if imageViews.count > 0 {
                    timeLabel.snp.remakeConstraints { make in
                        make.left.equalTo(nameLabel)
                        make.top.equalTo(imageViews.last!.snp.bottom).offset(13*widthScale)
                    }
                }else {
                    timeLabel.snp.remakeConstraints { make in
                        make.left.equalTo(nameLabel)
                        make.top.equalTo(contentLabel.snp.bottom).offset(13*widthScale)
                    }
                }
            }
            
        }
    }
    
    class func cellWithTableView(tableView:UITableView) -> (HWGameCommentSonTopCell) {
        let reUse:String = "HWGameCommentSonTopCell"
        
        var cell = tableView.dequeueReusableCell(withIdentifier: reUse)
        
        if cell == nil {
            cell = HWGameCommentSonTopCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: reUse)
        }
        
        return cell as! HWGameCommentSonTopCell
    }
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        initUI()
        initFrame()
    }
    
    func initUI() -> Void {
        selectionStyle = UITableViewCell.SelectionStyle.none
        backgroundColor = UIColor.clear
        
        headImageView = UIImageView()
        headImageView.layer.cornerRadius = 19*widthScale
        headImageView.contentMode = .scaleAspectFill
        headImageView.layer.masksToBounds = true
        contentView.addSubview(headImageView)
        
        nameLabel = GranibleLabel()
        nameLabel.font = .boldSystemFont(ofSize: 16*widthScale)
        contentView.addSubview(nameLabel)
        
        medalImageView = UIImageView.init()
        medalImageView.backgroundColor = .clear
        headImageView.contentMode = .scaleAspectFill
        contentView.addSubview(medalImageView)
        
        vipImageView = UIImageView.init()
        vipImageView.backgroundColor = .clear
        medalImageView.contentMode = .scaleAspectFill
        contentView.addSubview(vipImageView)
        
        headFrameImageView = UIImageView()
        headFrameImageView.backgroundColor = .clear
        headFrameImageView.layer.masksToBounds = true
        headFrameImageView.contentMode = .scaleAspectFill
        contentView.addSubview(headFrameImageView)
        
        starView = CosmosView.init()
        starView.rating = 0
        starView.settings.filledColor = .colorWithHexString(hexString: "#F97712")
        starView.settings.emptyColor = .clear
        starView.settings.emptyBorderColor = .clear
        starView.settings.starSize = 11*widthScale
        starView.settings.starMargin = 0
        starView.isUserInteractionEnabled = false
        starView.didFinishTouchingCosmos = {
            rating in
            
        }
        contentView.addSubview(starView)
        
        contentLabel = HWLabel.createLabel(title: "", color: "#333333", font: 14*widthScale)
        contentView.addSubview(contentLabel)

        videoImageView = UIImageView()
        videoImageView.layer.cornerRadius = 5*widthScale
        videoImageView.backgroundColor = .black
        videoImageView.layer.masksToBounds = true
        videoImageView.contentMode = .scaleAspectFill
        videoImageView.isUserInteractionEnabled = true
        contentView.addSubview(videoImageView)
        
        videoPlayImageView = UIImageView()
        videoPlayImageView.image = UIImage(named: "btn_video")
        videoPlayImageView.isHidden = true
        videoImageView.addSubview(videoPlayImageView)
        
        videoButton = UIButton(type: .custom)
        videoButton.addTarget(self, action: #selector(videoButtonClick(_:)), for: .touchUpInside)
        videoImageView.addSubview(videoButton)
        
        timeLabel = HWLabel.createLabel(title: "", color: "#999999", font: 14*widthScale)
        contentView.addSubview(timeLabel)
        
        commentImageView = UIImageView()
        commentImageView.image = UIImage(named: "gameCommentImage")
        contentView.addSubview(commentImageView)
        
        commentLabel = HWLabel.createLabel(title: "", color: "#999999", font: 14*widthScale)
        contentView.addSubview(commentLabel)
        
        zanImageView = UIImageView()
        zanImageView.image = UIImage(named: "zan_normal")
        contentView.addSubview(zanImageView)
        
        zanLabel = HWLabel.createLabel(title: "", color: "#999999", font: 14*widthScale)
        contentView.addSubview(zanLabel)
        
        lineImageView = UIImageView()
        lineImageView.image = UIImage(named: "commentLine")
        contentView.addSubview(lineImageView)
        
        zanButton = UIButton(type: .custom)
        zanButton.addTarget(self, action: #selector(zanButtonClick(_:)), for: .touchUpInside)
        contentView.addSubview(zanButton)
    }
    
    func initFrame() -> Void {
        headImageView.snp.makeConstraints { make in
            make.left.top.equalTo(15*widthScale)
            make.width.height.equalTo(38*widthScale)
        }
        
        headFrameImageView.snp.makeConstraints { make in
            make.center.equalTo(headImageView)
            make.width.height.equalTo(50*widthScale)
        }
        
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(headImageView.snp.right).offset(10*widthScale)
//            make.right.equalTo(-15*widthScale)
            make.top.equalTo(headImageView)
        }
        
        medalImageView.snp.makeConstraints { make in
            make.left.equalTo(nameLabel.snp.right).offset(6*widthScale)
            make.centerY.equalTo(nameLabel)
            make.width.height.equalTo(16*widthScale)
        }
        
        vipImageView.snp.makeConstraints { make in
            make.left.equalTo(medalImageView.snp.right).offset(6*widthScale)
            make.centerY.equalTo(medalImageView)
            make.width.height.equalTo(16*widthScale)
        }
        
        starView.snp.makeConstraints { make in
            make.left.equalTo(headImageView.snp.right).offset(10*widthScale)
            make.bottom.equalTo(headImageView).offset(-3*widthScale)
        }
        
        contentLabel.snp.makeConstraints { make in
            make.left.equalTo(headImageView.snp.right).offset(10*widthScale)
            make.top.equalTo(headImageView.snp.bottom).offset(10*widthScale)
            make.right.equalTo(-15*widthScale)
        }
        
        videoPlayImageView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.height.equalTo(40*widthScale)
        }
        
        videoButton.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        timeLabel.snp.makeConstraints { make in
            make.left.equalTo(nameLabel)
            make.top.equalTo(contentLabel.snp.bottom).offset(13*widthScale)
        }
        
        lineImageView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.height.equalTo(1*widthScale)
            make.top.equalTo(timeLabel.snp.bottom).offset(10*widthScale)
            make.bottom.equalTo(-10*widthScale)
        }
        
        zanImageView.snp.makeConstraints { make in
            make.width.height.equalTo(16*widthScale)
            make.centerY.equalTo(timeLabel)
            make.right.equalTo(zanLabel.snp.left).offset(-5*widthScale)
        }
        
        zanLabel.snp.makeConstraints { make in
            make.right.equalTo(-15*widthScale)
            make.centerY.equalTo(zanImageView)
        }
        
        commentImageView.snp.makeConstraints { make in
            make.width.height.equalTo(16*widthScale)
            make.centerY.equalTo(timeLabel)
            make.right.equalTo(commentLabel.snp.left).offset(-5*widthScale)
        }
        
        commentLabel.snp.makeConstraints { make in
            make.right.equalTo(zanImageView.snp.left).offset(-30*widthScale)
            make.centerY.equalTo(commentImageView)
        }
        
        zanButton.snp.makeConstraints { make in
            make.left.equalTo(zanImageView)
            make.right.equalTo(zanLabel)
            make.top.bottom.equalTo(zanImageView)
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    @objc func buttonClick(_ button:UIButton) -> Void {
        photoBroseView.imagesURL = model?.images
        photoBroseView.frameFormWindow = CGRect(x: screenWidth/2, y: screenHeight/2, width: 0, height: 0)
        photoBroseView.frameToWindow = CGRect(x: screenWidth/2, y: screenHeight/2, width: 0, height: 0)
        photoBroseView.currentIndex = button.tag - 9999
        photoBroseView.show()
    }
    
    @objc func zanButtonClick(_ button:UIButton) -> Void {
        self.zanButtonClick?(model?.id ?? "")
    }
    
    @objc func videoButtonClick(_ button:UIButton) -> Void {
        self.videoPlayClick?(self.model?.video ?? "")
    }
}
